/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/
package pl.vdl.azbest.mremote;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.prefs.Preferences;


import gnu.io.CommPortIdentifier;

import pl.vdl.azbest.annotations.InProgress;
import pl.vdl.azbest.annotations.Level;
import pl.vdl.azbest.annotations.TODO;
import pl.vdl.azbest.annotations.Team;
import pl.vdl.azbest.log.LoggingTheGathering;
import pl.vdl.azbest.log.LoggingTheGatheringUtil;

@InProgress
@TODO(target = "" + "<br>" + "add new config items as application grows<br>", responsible = Team.AZBEST, level = Level.IMPORTANT, dateAssigned = "21-03-2008")
public class Conf {
	{
		LoggingTheGathering.addPath(getClass().getName());
	}

	private volatile static Conf instance = null;

	public boolean consoleOpen = false;
    
	public boolean loggingTreeOpen = false;

	private int[] availableBaudRates = new int[] { 1200, 2400, 4800, 9600,
			14400, 19200, 38400, 57600, 115200, 256000, 460800, 921600 };

	private List<CommPortIdentifier> availableCommPorts = new ArrayList<CommPortIdentifier>();

	private CommPortIdentifier devicePort;
    private int GPRSCellQueringRateSeconds =2;//in seconds
	private int deviceBaudRate = 9600;
	private int GPRSCellQueringCount = 3;

	/** @deprecated */
	private String loggingRoot = "pl.vdl.azbest.comm.mc";

	private String fileSeparatorPropertyName = "file.separator";

	private String lineSeparatorPropertyName = "line.separator";

	public String LineS;

	public String PathS;

	private Logger logger = Logger.getLogger(getClass().getName()); // @jve:decl-index=0:

	/** Timeout in seconds */
	private int commConnectionTimeout = 10;// seconds

	static {
		Conf.getInstance().setFileSeparatorPropertyName("file.separator");
		Conf.getInstance().setLineSeparatorPropertyName("line.separator");
	}

	private String commPortName = "MC port";

	private Conf() {
		init();
	}

	private void init() {

	}

	public static Conf getInstance() {
		if (instance != null)
			return instance;
		else {
			synchronized (LoggingTheGatheringUtil.class) {
				instance = new Conf();
			}
			return instance;
		}
	}

	public List<CommPortIdentifier> getAvailableCommPorts() {
		return availableCommPorts;
	}

	public CommPortIdentifier getDevicePort() {
		return devicePort;
	}

	public void setDevicePort(CommPortIdentifier selectedPort) {
		this.devicePort = selectedPort;
	}

	public int getDeviceBaudRate() {
		return deviceBaudRate;
	}

	public void setDeviceBaudRate(int deviceBaudRate) {
		this.deviceBaudRate = deviceBaudRate;
	}

	public int[] getAvailableBaudRates() {
		return availableBaudRates;
	}

	/**
	 * @deprecated
	 */
	public String getPathSeparator() {
		return System.getProperty(fileSeparatorPropertyName);
	}

	public Handler[] getDefaultLoggerHandler() {
		return Logger.getLogger(getClass().getName()).getHandlers();
	}

	public String getLoggingRoot() {
		return loggingRoot;
	}

	public void setLoggingRoot(String loggingRoot) {
		this.loggingRoot = loggingRoot;
	}

	public String getFileSeparatorPropertyName() {
		return fileSeparatorPropertyName;
	}

	public void setFileSeparatorPropertyName(String fileSeparatorPropertyName) {
		this.fileSeparatorPropertyName = fileSeparatorPropertyName;
		Conf.getInstance().PathS = System
				.getProperty(this.fileSeparatorPropertyName);

	}

	public String getLineSeparatorPropertyName() {
		return lineSeparatorPropertyName;

	}

	public void setLineSeparatorPropertyName(String lineSeparatorPropertyName) {
		this.lineSeparatorPropertyName = lineSeparatorPropertyName;
		Conf.getInstance().LineS = System
				.getProperty(this.lineSeparatorPropertyName);
	}

	public void setAvailableCommPorts(
			List<CommPortIdentifier> availableCommPorts) {
		this.availableCommPorts.addAll(availableCommPorts);
	}

	public int getCommConnectionTimeout() {
		return commConnectionTimeout;
	}

	public void setCommConnectionTimeout(int commConnectionTimeout) {
		this.commConnectionTimeout = commConnectionTimeout;
	}

	public String getCommPortName() {
		return commPortName;
	}

	public void setCommPortName(String commPortName) {
		this.commPortName = commPortName;
	}

	public void saveConfigurationData() {
		Properties pref = new Properties();
		pref.setProperty("availableBaudRates",
				"1200,2400,4800,9600,14400,19200,"
						+ "38400,57600,115200,256000,460800," + "921600");
		pref.setProperty("deviceBaudRate", ("" + deviceBaudRate).trim());
		pref.setProperty("commConnectionTimeout", ("" + commConnectionTimeout)
				.trim());

		try {
			File file = new File("MRemote.xml");
			if (!file.exists())
				file.createNewFile();
			if (!file.canWrite())
				throw new IOException("Cant write to MRemote.xml file.");
			pref.storeToXML(new FileOutputStream(file),
					"MRemoteApplicationConfigFile");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void loadConfigurationData() {
		Properties pref = new Properties();
		try {
			File file = new File("MRemote.xml");
			if (!file.exists())
				throw new IOException(
						"There is no MRemote.xml config file. Cannot read application settings ");
			if (!file.canRead())
				throw new IOException("Cant read from MRemote.xml file.");

			pref.loadFromXML(new FileInputStream(file));
		} catch (IOException e) {
			e.printStackTrace();
		}
		// setting settings
		String availableBaudRates = pref.getProperty("availableBaudRates");
		StringTokenizer st = new StringTokenizer(availableBaudRates,",");
		this.availableBaudRates = new int[st.countTokens()];
		int i =0;
		if(st.hasMoreElements()){
			int v = Integer.parseInt((String)st.nextElement());
			this.availableBaudRates[i] =v;
			i++;
		}
		String deviceBaudRate = pref.getProperty("deviceBaudRate");
		this.deviceBaudRate = Integer.parseInt(deviceBaudRate);
		String commConnectionTimeout = pref.getProperty("commConnectionTimeout");
		this.commConnectionTimeout =  Integer.parseInt(commConnectionTimeout);

	}
	public static Properties lang ;
	public static void loadLanguage(String filename) throws InvalidPropertiesFormatException, FileNotFoundException, IOException{
		lang = new Properties();
		File f = new File(filename);

		lang.loadFromXML(new FileInputStream(f));
	}

	public int getGPRSCellQueringRateSeconds() {
		return GPRSCellQueringRateSeconds;
	}

	public void setGPRSCellQueringRateSeconds(int cellQueringRateSeconds) {
		GPRSCellQueringRateSeconds = cellQueringRateSeconds;
	}

	public int getGPRSCellQueringCount() {
		return GPRSCellQueringCount;
	}

	public void setGPRSCellQueringCount(int cellQueringCount) {
		GPRSCellQueringCount = cellQueringCount;
	}


}
